import { _decorator, Component, Node, Vec3 } from 'cc';
import { gameManager } from './gameManager';
import { audioManager } from '../framework/audioManager';
import { constant } from '../framework/constant';
import { resourceUtil } from '../framework/resourceUtil';
import { poolManager } from '../framework/poolManager';
const { ccclass, property } = _decorator;

@ccclass('arrow')
export class arrow extends Component {

    /** 箭是否自动调整角度 */
    public isAutoRotate: boolean = true;
    /** 箭是否弹射 */
    public isArrowLaunch: boolean = false;
    /** 箭是否在墙上反弹 */
    // public isArrowRebound: boolean = false;
    /** 箭反弹次数 */
    // public arrowReboundTimes: number = 0;

    /** 放弓箭特效的节点 */
    private _ndBody: Node = null!;
    /** 当前速度 */
    private _curSpeed: number = 0;
    /** 目标速度 */
    private _targetSpeed: number = 0;
    /** 初始默认位置 */
    private _oriPos: Vec3 = null!;
    /** 初始默认角度 */
    private _oriEulerAngles: Vec3 = null!
    /** 和玩家之间的向量差 */
    private _offsetPos: Vec3 = new Vec3();
    /** 当前节点世界坐标 */
    private _curWorPos: Vec3 = new Vec3();
    /** 箭节点超过玩家这个范围则隐藏 */
    private _disappearRange: number = 25;
    /** 是否已经加载完所有特效 */
    private _isLoadEffectOver: boolean = false;
    /** 是否需要特效 */
    private _isNeedShowEffect: boolean = false;
    /** 箭的下次目标位置 */
    private _targetWorPos: Vec3 = new Vec3();
    /** 当前角度 */
    private _curEulerAngles: Vec3 = new Vec3();
    /** 初始朝向 */
    private _oriForward: Vec3 = null!;
    /** 当前朝向 */
    private _curForward: Vec3 = new Vec3();
    /** 技能释放位置的世界坐标 */
    private _releaseWorPos: Vec3 = new Vec3();
    /** 向量差 */
    private _offsetPos_1: Vec3 = new Vec3();
    /** 向量差 */
    private _offsetPos_2: Vec3 = new Vec3();
    /** 两个向量叉乘 */
    private _cross: Vec3 = new Vec3();


    /**
    * 初始化 
    */
    public init(speed: number, releaseWorPos: Vec3) {
        this._releaseWorPos.set(releaseWorPos);

        if (!this._ndBody) {
            this._ndBody = this.node.getChildByName("body") as Node;
        }

        this._isLoadEffectOver = false;
        this._isNeedShowEffect = false;

        this.isArrowLaunch = false;
        // this.isArrowRebound = false;
        // this.arrowReboundTimes = 0;

        if (!this._oriPos) {
            this._oriPos = this.node.position.clone();
        }

        if (!this._oriEulerAngles) {
            this._oriEulerAngles = this.node.eulerAngles.clone();
        }

        if (!this._oriForward) {
            this._oriForward = this.node.forward.clone();
        }

        this.node.active = false;
        this.node.setPosition(this._oriPos);
        this.node.eulerAngles = this._oriEulerAngles;
        this._curForward.set(this._oriForward);

        this._targetSpeed = speed;
        this._curSpeed = speed * 0.5;

        this._ndBody.children.forEach((ndChild: Node) => {
            if (ndChild.name.startsWith("arrow")) {
                ndChild.active = false;
            }
        })

        let isHasIce = gameManager.scriptPlayer.isArrowIce;
        let isHasFire = gameManager.scriptPlayer.isArrowFire;
        let isHasLightning = gameManager.scriptPlayer.isArrowLightning;

        //根据玩家拥有的不同技能展示对应特效
        if (isHasFire || isHasIce || isHasLightning) {
            this._isNeedShowEffect = true;

            if (isHasFire && isHasIce && isHasLightning) {
                this._showTrail("arrowAll");
            } else {
                if (isHasFire && isHasIce || isHasFire && isHasLightning || isHasIce && isHasLightning) {
                    if (isHasFire && isHasIce) {
                        this._showTrail("arrowFireIce");
                        audioManager.instance.playSound(constant.SOUND.FIRE);
                    } else if (isHasLightning && isHasFire) {
                        this._showTrail("arrowLightningFire");
                        audioManager.instance.playSound(constant.SOUND.LIGHTNING);
                    } else if (isHasLightning && isHasIce) {
                        this._showTrail("arrowLightningIce");
                        audioManager.instance.playSound(constant.SOUND.ICE);
                    }
                } else {
                    if (isHasFire) {
                        this._showTrail("arrowFire");
                        audioManager.instance.playSound(constant.SOUND.FIRE);
                    } else if (isHasIce) {
                        this._showTrail("arrowIce");
                        audioManager.instance.playSound(constant.SOUND.ICE);
                    } else if (isHasLightning) {
                        this._showTrail("arrowLightning");
                        audioManager.instance.playSound(constant.SOUND.LIGHTNING);
                    }
                }
            }
        } else {
            //不展示特效
            this._ndBody.children.forEach((ndChild: Node) => {
                if (ndChild.name.startsWith("arrow")) {
                    ndChild.active = false;
                }
            })

            this.node.active = true;

            if (gameManager.isGameStart) {
                audioManager.instance.playSound(constant.SOUND.LOOSE);
            }
        }
    }


    /**
     * 展示箭的特效拖尾
     *
     * @private
     * @param {string} effectName
     * @memberof Arrow
     */
    private _showTrail(effectName: string) {
        let ndTrail: Node | null = this._ndBody.getChildByName(effectName);
        if (ndTrail) {
            ndTrail.active = true;
            this.node.active = true;
            this._isLoadEffectOver = true;
        } else {
            resourceUtil.loadEffectRes(`arrow/${effectName}`).then((pf: any) => {
                ndTrail = poolManager.instance.getNode(pf, this._ndBody);
                this.node.active = true;
                this._isLoadEffectOver = true;
            });
        }
    }
}


